#include "../import/head.h"
#include "helper_proc.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_protocol/head.h"
#include "../helper_package/head.h"
#include "../helper_heart/head.h"
#include "../helper_http/head.h"
#include "../helper_net/head.h"
#include "../helper_tcp/head.h"
#include "../helper_udp/head.h"
#include "proc_misc.h"

namespace xos_sgsj
{

    helper_proc::helper_proc()
    {
    }

    helper_proc::~helper_proc()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int helper_proc::proc( data * pData )
    {
        int ret = 0;

        int nMsg = pData->m_nMsg;

        switch( nMsg )
        {
                // objects
            case MSG_PACKAGE:
                {
                    helper_package obj;
                    ret = obj.proc( pData->m_pMsg );
                }
                break;
            case MSG_HTTP:
                {
                    helper_http obj;
                    ret = obj.proc( pData->m_pMsg );
                }
                break;
            case MSG_NET:
                {
                    helper_net obj;
                    ret = obj.proc( pData->m_pMsg );
                }
                break;

                // heart
            case MSG_HEART:
                {
                    helper_heart obj;
                    ret = obj.proc();
                }
                break;

			case MSG_SESSION_REF:
				{
					ret = proc_session_ref( pData );
				}
				break;

                // program msg
            case MSG_START_PROGRAM:
            case MSG_HTTP_SERVER_STARTED:
            case MSG_TCP_SERVER_STARTED:
            case MSG_UDP_SERVER_STARTED:
            case MSG_PROGRAM_STARTED:
            case MSG_NET_STOPPED:
            case MSG_QUITING:
            case MSG_QUITED:
                {
                    proc_misc obj;
                    ret = obj.proc( pData );
                }
                break;

                // tcp msg
            case MSG_TCP_NEED_CLOSE:
            case MSG_TCP_CLOSE:
            case MSG_TCP_INIT:
            case MSG_TCP_ACCEPT:
            case MSG_TCP_CONNECT:
            case MSG_TCP_RECV:
            case MSG_TCP_SEND:
                {
                    helper_tcp obj;
                    ret = obj.proc( pData );
                }
                break;

                // udp msg
			case MSG_UDP_NEED_CLOSE:
            case MSG_UDP_CLOSE:
            case MSG_UDP_INIT:
            case MSG_UDP_RECV:
            case MSG_UDP_SEND:
                {
                    helper_udp obj;
                    ret = obj.proc( pData );
                }
                break;

            default:
                {
                    ret = 1;
                }
                break;
        }

        if( pData )
        {
            pData->release();
            pData = 0;
        }

        return ret;
    }

	int helper_proc::proc_session_ref( data * pData )
	{
		int ret = 0;

		session * pSession = pData->m_pSession;

		if( pData->m_nRef > 0 )
		{
			pSession->add_ref( pData->m_nRef );
		}
		else
		{
			pSession->sub_ref( -pData->m_nRef );
		}

		return ret;
	}

} // xos_sgsj
